package com.wbao.dianniu.db;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteOpenHelper extends SQLiteOpenHelper {

    public static final String USER_TAB = "user";
    public static final String CONFIG_TAB = "config";
    public static final String KNOWLEDGE_TAB = "knowledge";
    //数据库版本号
    private static Integer Version = 1;

    private final String CREAT_KNOWLEDGE_TAB = "CREATE TABLE IF NOT EXISTS " +
            KNOWLEDGE_TAB + "(" +
            KnowInfo.ACCOUNDID + " integer primary key," +
            KnowInfo.SUBJECT + " varchar(200)," +
            KnowInfo.CONTENT + " text," +
            KnowInfo.NOTICEJSON + " varchar(200)," +
            KnowInfo.STATUS + " integer," +
            KnowInfo.DATE + " varchar(50)" +
            ")";

//    private final String CREAT_USER_TAB = "CREATE TABLE IF NOT EXISTS " +
//            USER_TAB + "(" +
//            UserInfo.ID + " integer primary key," +
//            UserInfo.MOBILE + " varchar," +
//            UserInfo.TOKEN + " varchar," +
//            UserInfo.USERNAME + " varchar," +
//            UserInfo.PASSWORD + " varchar," +
//            UserInfo.USERICON + " blob" +
//            ")";
//    private final String CREAT_CONFIG_TAB = "CREATE TABLE IF NOT EXISTS " +
//            CONFIG_TAB + "(" +
//            ConfigInfo.ID + " integer primary key," +
//            ConfigInfo.SERVICE_URL + " varchar," +
//            ConfigInfo.SERVICE_PORT + " integer" +
//            ")";

    public MySQLiteOpenHelper(Context context, String name, CursorFactory factory,
                              int version) {
        super(context, name, factory, version);
    }

    //参数说明
    //context:上下文对象
    //name:数据库名称
    //param:factory
    //version:当前数据库的版本，值必须是整数并且是递增的状态

    public MySQLiteOpenHelper(Context context, String name, int version) {
        this(context, name, null, version);
    }


    public MySQLiteOpenHelper(Context context, String name) {
        this(context, name, Version);
    }

    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
//        db.execSQL(CREAT_USER_TAB);
//        db.execSQL(CREAT_CONFIG_TAB);
        //execSQL用于执行SQL语句
        //完成数据库的创建
        //数据库实际上是没有被创建或者打开的，直到getWritableDatabase() 或者 getReadableDatabase() 方法中的一个被调用时才会进行创建或者打开
        db.execSQL(CREAT_KNOWLEDGE_TAB);
        Log.e("Database", "onCreate");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + KNOWLEDGE_TAB);
        onCreate(db);
        Log.e("Database", "onUpgrade");
    }


}
